<?php

namespace api\modules\v1\controllers;

use api\controllers\OnAuthController;
use api\modules\v1\forms\base\JobForm;
use common\helpers\CommonHelper;
use common\models\base\Job;
use services\base\JobService;
use Yii;

/**
 * Class JobController
 * @package api\modules\v1\controllers
 */
class JobController extends OnAuthController
{
    public $modelClass = Job::class;


    /**
     * @api {get} /v1/job/list  岗位列表
     * @apiName list
     * @apiGroup Job
     * @apiUse CommonParam
     *
     * @apiParam {Number} status            状态
     * @apiParam {String} page              页码
     * @apiParam {String} pageSize          每页展示数
     *
     * @apiSuccess {Object}  data                       返回数据
     * @apiSuccess {Number}  data.page                  页码
     * @apiSuccess {Number}  data.pageSize              每页展示数
     * @apiSuccess {Number}  data.total                 总数
     * @apiSuccess {Array}   data.list                  列表数据
     * @apiSuccess {Number}  data.list.id               用户ID
     * @apiSuccess {String}  data.list.job_name         岗位名称
     * @apiSuccess {String}  data.list.sort             排序
     * @apiSuccess {String}  data.list.status_str       状态
     */
    public function actionList()
    {
        $params = Yii::$app->request->get();

        $page = $params['page'] ?? 1;
        $pageSize = $params['pageSize'] ?? 10;

        $service = new JobService();
        // 查询结果
        $data = $service->getListPage($params, (int)$page, (int)$pageSize);
        // 格式化数据
        $data['list'] = $service->getListPageFormat($data['list']);
        return $data;
    }

    /**
     * @api {get} /v1/job/info 岗位详情
     * @apiName info
     * @apiGroup Job
     * @apiUse CommonParam
     *
     * @apiParam {Number} id 岗位ID【新增ID不传，编辑必传ID】
     *
     * @apiSuccess {Object} data                    返回数据
     * @apiSuccess {Number} data.id                 ID
     * @apiSuccess {Number} data.job_name           岗位名称
     * @apiSuccess {Number} data.status             状态
     * @apiSuccess {Number} data.sort               排序
     */
    public function actionInfo()
    {
        $params = Yii::$app->request->get();
        $id = $params['id'] ?? 0;

        // 查询结果
        $service = new JobService();
        return $service->getInfo((int)$id);
    }

    /**
     * @api {post} /v1/job/create 新增岗位
     * @apiName create
     * @apiGroup Job
     * @apiUse CommonParam
     *
     * @apiBody {String} job_name          岗位名称
     * @apiBody {Number} sort              排序【选填，不能小于0】
     * @apiBody {Number} status            岗位状态【1启用、0停用】
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         岗位ID
     */
    public function actionCreate()
    {
        $params = Yii::$app->request->post();
        $params['sort'] = intval($params['sort'] ?? 0);

        $model = new JobForm();
        $model->setScenario('create');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $service = new JobService();
        return $service->create($model);
    }

    /**
     * @api {post} /v1/job/update 编辑岗位
     * @apiName update
     * @apiGroup Job
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                岗位ID
     * @apiBody {String} job_name          岗位名称
     * @apiBody {Number} sort              排序【选填，不能小于0】
     * @apiBody {Number} status            岗位状态【1启用、0停用】
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         岗位ID
     */
    public function actionUpdate()
    {
        $params = Yii::$app->request->post();
        $params['sort'] = intval($params['sort'] ?? 0);

        $model = new JobForm();
        $model->setScenario('edit');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $service = new JobService();
        return $service->edit($model);
    }

    /**
     * @api {post} /v1/job/delete 删除岗位
     * @apiName delete
     * @apiGroup Job
     * @apiUse CommonParam
     *
     * @apiBody {Number} id                岗位ID
     *
     * @apiSuccess {Object} data            返回数据
     * @apiSuccess {Number} data.id         岗位ID
     */
    public function actionDelete()
    {
        $params = Yii::$app->request->post();

        $model = new JobForm();
        $model->setScenario('delete');
        $model->attributes = $params;
        if (!$model->validate()) {
            // 返回数据验证失败
            return CommonHelper::api(422, $this->getError($model));
        }

        $service = new JobService();
        return $service->delete($model);
    }
}
